/*
 * Copyright 2019 Flipkart Internet Pvt. Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flipkart.ohos.proteus.processor;

import ohos.agp.components.Component;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import ohos.global.resource.solidxml.Theme;

import com.flipkart.ohos.proteus.ProteusContext;
import com.flipkart.ohos.proteus.ProteusView;
import com.flipkart.ohos.proteus.value.AttributeResource;
import com.flipkart.ohos.proteus.value.Dimension;
import com.flipkart.ohos.proteus.value.Resource;
import com.flipkart.ohos.proteus.value.StyleResource;
import com.flipkart.ohos.proteus.value.Value;

import java.io.IOException;

public abstract class DimensionAttributeProcessor<T extends Component> extends AttributeProcessor<T> {
    public static float evaluate(Value value, ProteusView view) {
        if (value == null) {
            return Dimension.ZERO.apply(view.getAsView().getContext());
        }

        final float[] result = new float[1];
        DimensionAttributeProcessor<Component> processor = new DimensionAttributeProcessor<Component>() {
            @Override
            public void setDimension(Component view, float dimension) {
                result[0] = dimension;
            }
        };
        processor.process(view.getAsView(), value);

        return result[0];
    }

    public static Value staticCompile(Value value, Context context) {
        if (null == value || !value.isPrimitive()) {
            return Dimension.ZERO;
        }
        if (value.isDimension()) {
            return value;
        }
        Value precompiled = AttributeProcessor.staticPreCompile(value.getAsPrimitive(), context, null);
        if (null != precompiled) {
            return precompiled;
        }
        return Dimension.valueOf(value.getAsString());
    }

    @Override
    public final void handleValue(T view, Value value) {
        if (value.isDimension()) {
            setDimension(view, value.getAsDimension().apply(view.getContext()));
        } else if (value.isPrimitive()) {
            process(view, precompile(value, view.getContext(),
                ((ProteusContext) view.getContext()).getFunctionManager()));
        }
    }

    @Override
    public void handleResource(T view, Resource resource) {
        Float dimension = resource.getDimension(view.getContext());
        setDimension(view, null == dimension ? 0 : dimension);
    }

    @Override
    public void handleAttributeResource(T view, AttributeResource attribute) {
        Theme theme = attribute.applyTheme(view.getContext());
        try {
            setDimension(view, theme.getPixelValue(view.getName(), false));
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void handleStyleResource(T view, StyleResource style) {
        Theme theme = style.applyTheme(view.getContext());
        try {
            setDimension(view, theme.getPixelValue(view.getName(), false));
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.printStackTrace();
        }
    }

    public abstract void setDimension(T view, float dimension);

    @Override
    public Value compile(Value value, Context context) {
        return staticCompile(value, context);
    }
}
